#include "stdafx.h"
#include "LoggedTestSuite.h"

LoggedTestSuite::LoggedTestSuite() :
	testName_(),
	testIndex_(0)
{
}

void LoggedTestSuite::setup()
{
	std::string testName = this->testCaseFileName();
	CO::SpiFoundation::RoomFW::SimpleLogger& logger = CO::SpiFoundation::RoomFW::SimpleLogger::Instance();
	TEST_ASSERT(!logger.isRunning());
	logger.setConsole(false);
	logger.setFile(true, (testName + ".log").c_str());
	logger.setVerbosity(CO::SpiFoundation::RoomFW::LV_ALL);
	TEST_ASSERT(logger.start(DWait));
	TEST_ASSERT(logger.isRunning(DWait));
	G_LOG_1(CO::SpiFoundation::RoomFW::LV_SYSTEM, "Test case %s begin", testName.c_str());
}

void LoggedTestSuite::tear_down()
{
	G_LOG_1(CO::SpiFoundation::RoomFW::LV_SYSTEM, "Test case %s end", this->testCaseFileName().c_str());
	CO::SpiFoundation::RoomFW::SimpleLogger& logger = CO::SpiFoundation::RoomFW::SimpleLogger::Instance();
	TEST_ASSERT(logger.stop(DWait));
	TEST_ASSERT_MSG(!logger.isErrored(), logger.errorMessage().c_str());
	++testIndex_;
}

std::string LoggedTestSuite::testCaseFileName()
{
	std::string retVal;
	if (testIndex_ < testName_.size())
	{
		retVal = testName_[testIndex_];
		retVal.replace(retVal.find("::"), 2, "_");
	}
	else
	{
		retVal = "Error";
		char buf[64];
		retVal += ::ltoa(testIndex_, buf, 10);
	}
	return retVal;
}